package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.HomeNewProductApi;
import com.yoda.mall.model.entity.sms.HomeNewProduct;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.HomeNewProductMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 12月 17日 0:25
 */
@DubboService
public class HomeNewProductApiImpl implements HomeNewProductApi {
    @Resource
    private HomeNewProductMapper homeNewProductMapper;

    @Override
    public PageResult<HomeNewProduct> selectNewProductByPage(Integer page, Integer pageSize, String productName, Integer recommendStatus) {
        Page<HomeNewProduct> productPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<HomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(productName), HomeNewProduct::getProductName,productName);
        wrapper.eq(Objects.nonNull(recommendStatus), HomeNewProduct::getRecommendStatus,recommendStatus);
        wrapper.orderByDesc(HomeNewProduct::getSort);
        homeNewProductMapper.selectPage(productPage, wrapper);
        return new PageResult<>(page, pageSize, (int) productPage.getTotal(), productPage.getRecords());
    }

    @Override
    public void changeNewProductSort(Long id, Integer sort) {
        HomeNewProduct newProduct = new HomeNewProduct();
        newProduct.setId(id);
        newProduct.setSort(sort);
        homeNewProductMapper.updateById(newProduct);
    }

    @Override
    public void updateNewProductStatus(List<Long> ids, Integer recommendStatus) {
        homeNewProductMapper.updateNewProductStatus(ids,recommendStatus);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        homeNewProductMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer createNewProduct(List<HomeNewProduct> homeNewProducts) {
        return homeNewProductMapper.saveBatch(homeNewProducts);
    }

}
